<template>
  <div>
    <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in list" :key="item.id" :value="item.username" :label="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getEmployeesSimple } from '@/api/employees'
import { addDepartment, editDepartmentById, getDepartmentByIdDetail } from '@/api/department'
export default {
  props: {
    pid: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 部门编码不能一样
    const checkCode = (rule, value, callback) => {
      let mapList = this.originList.map(item => item.code)
      if (this.isEdit) {
        mapList = this.originList.filter((item) => item.id !== this.pid).map((item) => item.code)
      }
      mapList.includes(value) ? callback(`${value}已存在,请重新输入`) : callback()
    }
    // 子部门的名称不能一致
    const vaildName = (rule, value, callback) => {
      console.log('this.originList', this.originList)
      let exsitName = this.originList.filter((item) => item.pid === this.pid).map((item) => item.name)
      if (this.isEdit) {
        const pid = this.originList.find(item => item.id === this.pid).pid
        exsitName = this.originList.filter(item => item.pid === pid).filter(item => item.id !== this.pid).map(item => item.name)
      }
      console.table(exsitName)
      exsitName.includes(value) ? callback('部门名重复,请重新输入') : callback()
    }

    return {
      // 表单验证规则
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' },
          { validator: vaildName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],
        manager: [
          { type: 'string', required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { type: 'string', required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]
      },
      // 员工简单列表
      list: []
    }
  },
  watch: {
    pid() {
      if (this.isEdit) this.loadDepartmentByIdDetail()
    }
  },
  created() {
    this.loadEmployeesSimple()
    if (this.isEdit) this.loadDepartmentByIdDetail()
  },
  methods: {
    // 重置表单
    resetForm() {
      this.$refs.deptForm.resetFields()
    },
    // 获取员工简单列表
    async loadEmployeesSimple() {
      try {
        const res = await getEmployeesSimple()
        console.log('员工简单列表', res)
        this.list = res.data
      } catch (error) {
        console.log(error)
      }
    },
    // 增加子部门
    async loadAddDepartment() {
      try {
        const res = await addDepartment({ pid: this.pid, ...this.form })
        console.log('增加子部门', res)
        // 通知父组件更新数据
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 修改子部门
    async loadEditDepartmentById() {
      try {
        const res = await editDepartmentById(this.form)
        console.log('修改子部门', res)
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 根据id获取详情
    async loadDepartmentByIdDetail() {
      try {
        const res = await getDepartmentByIdDetail(this.pid)
        console.log('根据id获取详情', res)
        this.form = res.data
      } catch (error) {
        console.log(error)
      }
    },
    hSubmit() {
      this.isEdit ? this.loadEditDepartmentById() : this.loadAddDepartment()
    },
    hCancel() {
      this.$emit('close')
    }

  }
}

</script>
